<?php

namespace Application\Controller;

use Zend\Form\Annotation\AnnotationBuilder;
use Application\Controller\IndexController;
use Application\Model\DBO\ProcessReaderDBO;
use Application\Form\ChangePasswordForm;
use Application\Form\SubmitForm;
use Application\Model\DBO\BookingOnlineDBO;
use Admin\Entity\Reader;
use Application\Form\UpdateInfoReaderForm;

class PersonalController extends IndexController {

    public $form;

    public function getForm() {
        if (!$this->form) {
            $changePassword = new ChangePasswordForm();
            $builder = new AnnotationBuilder();
            $this->form = $builder->createForm($changePassword);
        }

        return $this->form;
    }

    public function indexAction() {
        // Kiem tra quyen
        $this->checkRole();
        // Lấy thông tin tài liệu bạn đọc đang mượn
        $cardBarcode = $this->getAuthService()->getStorage()->read();
        $objectProcessReader = new ProcessReaderDBO($this->getServiceLocator());
        $listBookBorrow = $objectProcessReader->getListBookBorrow($cardBarcode);
        $paginator = $listBookBorrow['paginator'];
        $allAuthor = $listBookBorrow['allAuthor'];
        $paginator->setCurrentPageNumber((int) $this->params()->fromQuery('page', 1));
        $paginator->setItemCountPerPage(7);
        $paginator->setPageRange(5);
        $this->layout('layout/layout-left');
        return array(
            'allAuthor' => $allAuthor,
            'paginator' => $paginator,
        );
    }

    /*
     * Lấy thông tin người bạn đọc
     */

    public function orderOnlineAction() {
        $this->layout('layout/layout-left');
        $cardBarcode = $this->getAuthService()->getStorage()->read();
        $objectBookingOnlineDBO = new BookingOnlineDBO($this->getServiceLocator());
        $data = $objectBookingOnlineDBO->getBookOrder($cardBarcode);
        return $data;
    }

    public function infoReaderAction() {
        // Kiem tra quyen nguoi dung
        $this->checkRole();
        $this->layout('layout/layout-left.phtml');
        // Lấy thông tin về người dùng.
        $builder = new AnnotationBuilder();
        $form = $builder->createForm(new SubmitForm());
        $objectProcessReader = new ProcessReaderDBO($this->getServiceLocator());
        $infoReader = $objectProcessReader->getInfoReader($this->getAuthService()->getIdentity());
        return array(
            'form' => $form,
            'infoReader' => $infoReader,
        );
    }

    /*
     * Thay đổi mật khẩu người dùng
     */

    public function changePasswordAction() {
        // Kiem tra quyen nguoi dung
        $this->layout('layout/layout-left.phtml');
        $this->checkRole();
        $form = $this->getForm();
        $request = $this->getRequest();
        $myMessage = array();
        if ($request->isPost()) {
            $inputData = $request->getPost();
            $form->setData($inputData);
            if ($form->isValid()) {
                $data = $form->getData();
                $oldPass = $data['oldPassword'];
                $newPass = $data['newPassword'];
                $confirmPass = $data['confirmPassword'];
                if ($oldPass == $newPass || $newPass != $confirmPass) {
                    $myMessage['changePassError'] = 'changePassError';
                } else {
                    $objectProcessReader = new ProcessReaderDBO($this->getServiceLocator());
                    $result = $objectProcessReader->changePassword($this->getAuthService()->getIdentity(), $newPass);
                    if ($result) {
                        $myMessage['changePassOK'] = 'changePassOK';
                    } else {
                        $myMessage['changePassError'] = 'changePassError';
                    }
                }
            } else {
                foreach ($form->getMessages() as $value => $ms) {
                    $myMessage[$value] = $value;
                }
            }
            return array(
                'form' => $form,
                'myMessage' => $myMessage,
            );
        }
        return array(
            'form' => $form,
        );
    }

    /*
     * Gia hạn sách mượn
     */

    public function addTimeBorrowAction() {
        // So sánh với thời gian gia hạn tối đa từ chính sách lưu thông
        $this->checkRole();
        $myMessage = 'addTimeError';
        $objectProcessReader = new ProcessReaderDBO($this->getServiceLocator());
        $dataBook = $this->params()->fromRoute('id', 0);
        $result = $objectProcessReader->addTimeBorrow($dataBook);
        if ($result) {
            $myMessage = 'addTimeOK';
        }
        // Lấy danh sách tài liệu đang mượn
        $cardBarcode = $this->getAuthService()->getStorage()->read();
        $listBookBorrow = $objectProcessReader->getListBookBorrow($cardBarcode);
        $paginator = $listBookBorrow['paginator'];
        $allAuthor = $listBookBorrow['allAuthor'];
        $paginator->setCurrentPageNumber((int) $this->params()->fromQuery('page', 1));
        $paginator->setItemCountPerPage(7);
        $paginator->setPageRange(5);

        return array(
            'myMessage' => $myMessage,
            'paginator' => $paginator,
            'allAuthor' => $allAuthor,
        );
    }

    public function updateInfoReaderAction() {
        $userID = $this->params()->fromRoute('id', 0);
        $this->layout('layout/layout-left.phtml');
        // Lấy thông tin bạn đọc  với userID tương ứng
        if (!$userID) {
            return $this->redirect()->toRoute('personal', array(
                        'action' => 'infoReader'
            ));
        }
        $objectProcessReader = new ProcessReaderDBO($this->getServiceLocator());
        $infoReader = $objectProcessReader->getInfoReader($this->getAuthService()->getIdentity());
        $reader = new Reader();
        $reader->setDataReader($infoReader);
        $builder = new AnnotationBuilder();
        $form = $builder->createForm(new UpdateInfoReaderForm());
        // bind() để đưa ra form
        $form->bind($reader);
        $request = $this->getRequest();
        if ($request->isPost()) {
            $form->setData($request->getPost());
            if ($form->isValid()) {
                $data = $form->getData();
                $objectProcessReader = new ProcessReaderDBO($this->getServiceLocator());
                $objectProcessReader->updateInfoUser($reader, $userID);
                $this->redirect()->toRoute('personal/process', array('action' => 'infoReader'));
            } else {
                // Dua cac message error ra view
                foreach ($form->getMessages() as $value => $ms) {
                    $myMessage[$value] = $value;
                }
            }
            return array(
                'userID' => $infoReader['userID'],
                'form' => $form,
            );
        }
    }

}

?>
